package UIComponents.kfhd
{

	import Classes.GameUtils.Movement.MovementEvent;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;
	
	public class KfhdPanelUI extends Sprite
	{
		private var _list:Sprite;
		private var listBtn1:Sprite;
		private var listBtn2:Sprite;
		private var listBtn3:Sprite;
		private var listBtn4:Sprite;
		private var txtPage:TextField;
		private var leftBtn:Sprite;
		private var rightBtn:Sprite;
		
		private var movementAry:Array;
		private var nowPage:uint=1;
		private var maxPage:uint=1;
		
		public var nowSelectListObj:Object=null;
		
		/**
		 *杀僵尸数 (当天积分）
		 */
		public var jsdiecount:uint=0;
		
		/**
		 *未领的奖品 
		 */
		public var jsdieNoReward:String="";
		/**
		 *已领的奖品 
		 */
		public var jsdieYesReward:String="";
		
		/**
		 *消费点数 
		 */
		public var xfCount:uint=0;
		
	
		
		public function KfhdPanelUI()
		{
			_list	=	this.getChildByName("list") as Sprite;
			var closeSp:Sprite	=	new Sprite();
			this.addChild(closeSp);
			closeSp.x	=	this.getChildByName("closeBtn").x;
			closeSp.y	=	this.getChildByName("closeBtn").y;
			closeSp.addChild( new closeBtn_v);
			closeSp.addEventListener(MouseEvent.CLICK,clickCloseHandler);
			txtPage	=	this.getChildByName("txtPage") as TextField;
			
			leftBtn		=	new Sprite;
			rightBtn	=	new Sprite;
			this.addChild(leftBtn);
			this.addChild(rightBtn);

			leftBtn.x	=	this.getChildByName("leftBtnUI").x;
			leftBtn.y	=	this.getChildByName("leftBtnUI").y;
			this.getChildByName("leftBtnUI").visible	=	false;
			leftBtn.addChild(new leftBtn_v);
			rightBtn.x	=	this.getChildByName("rightBtnUI").x;
			rightBtn.y	=	this.getChildByName("rightBtnUI").y;
			this.getChildByName("rightBtnUI").visible	=	false;
			rightBtn.addChild( new leftBtn_v);
			rightBtn.scaleX=-1;
			
			
			leftBtn.addEventListener(MouseEvent.CLICK,clickLeftBtnHandler);
			rightBtn.addEventListener(MouseEvent.CLICK,clickRightBtnHandler);
				
			_list.getChildByName("btn1").visible=false;
			_list.getChildByName("btn2").visible=false;
			_list.getChildByName("btn3").visible=false;
			_list.getChildByName("btn4").visible=false;
			

			var btnbg1:Sprite	=	_list.getChildByName("listBtn1") as Sprite;
			var btnbg2:Sprite	=	_list.getChildByName("listBtn2") as Sprite;
			var btnbg3:Sprite	=	_list.getChildByName("listBtn3") as Sprite;
			var btnbg4:Sprite	=	_list.getChildByName("listBtn4") as Sprite;
			
			
			listBtn1	=	new Sprite;
			listBtn2	=	new Sprite;
			listBtn3	=	new Sprite;
			listBtn4	=	new Sprite;
			
			listBtn1.x	=	btnbg1.x;
			listBtn1.y	=	btnbg1.y;
			listBtn2.x	=	btnbg2.x;
			listBtn2.y	=	btnbg2.y;
			listBtn3.x	=	btnbg3.x;
			listBtn3.y	=	btnbg3.y;
			listBtn4.x	=	btnbg4.x;
			listBtn4.y	=	btnbg4.y;
			
			
			listBtn1.addEventListener(MouseEvent.CLICK, clickBtnHandler1);
			listBtn2.addEventListener(MouseEvent.CLICK, clickBtnHandler2);
			listBtn3.addEventListener(MouseEvent.CLICK, clickBtnHandler3);
			listBtn4.addEventListener(MouseEvent.CLICK, clickBtnHandler4);
			
			
			var ary:Array = new Array();
			ary.push( {btnClassName:"btn_wljs",contentClass:"content_wljs",btn1:"围猎僵尸",btn2:"围猎僵尸"});
			ary.push( {btnClassName:"btn_xfsl",contentClass:"content_xfsdl",btn1:"消费送礼",btn2:"消费"});
			ary.push( {btnClassName:"btn_xykd",contentClass:"content_xykd",btn1:"邀请好友",btn2:"领取奖励",btn3:"寻友抗敌更多"});
			ary.push( {btnClassName:"btn_wscq",contentClass:"content_wscq",btn1:"开启礼包",btn2:"我是传奇更多"});
			ary.push( {btnClassName:"btn_qrdl",contentClass:"content_qrdl",btn1:"七天大礼",btn2:"七日大礼更多"});
			
//			ary.push( {btnClassName:"btn_wljs",contentClass:"content_wljs",btn1:"围猎僵尸",btn2:"围猎僵尸"});
//			ary.push( {btnClassName:"btn_yzwd",contentClass:"content_yzwd",btn1:"围猎僵尸",btn2:"围猎僵尸"});
			

			this.setMovement(ary);
//			setContent( (movementAry[ 0] as Object).contentClass);
			this.clickBtnHandler1(null);
			this.addBtnsListener();
		}
		
		public function setMovement(ary:Array):void
		{
			this.maxPage	=	Math.floor( (ary.length-1)/4) + 1;
			this.movementAry	=	new Array;
			this.movementAry	=	ary;
			this.cleanList();
			for( var i:uint=0; i<4; i++)
			{
				if( (nowPage-1)*4+i >= this.movementAry.length)break;
				this.setListBtn( (i+1).toString(),  (movementAry[ (nowPage-1)*4+i] as Object).btnClassName);
				
				
				if(this.nowSelectListObj	==	(movementAry[ (nowPage-1)*4+i] as Object))
					setGlowFilter(i+1);
			}
			
			this.txtPage.text	=	nowPage+"/"+maxPage;
			this.txtPage.x =	( this.getChildByName("txtbg") as Sprite).x+10;
		}
		
		
		public function addBtnsListener():void
		{
			var sp1:Sprite	=	this.getChildByName("content_xykd") as Sprite;
			var sp2:Sprite	=	this.getChildByName("content_qrdl") as Sprite;
			var sp3:Sprite	=	this.getChildByName("content_wscq") as Sprite;
			var sp4:Sprite	=	this.getChildByName("content_wljs") as Sprite;

			for(var i:uint=1; i<5; i++)
			{
				if(sp1.getChildByName("btn"+i))sp1.getChildByName("btn"+i).addEventListener(MouseEvent.CLICK,clickBtnHandler);
				if(sp2.getChildByName("btn"+i))sp2.getChildByName("btn"+i).addEventListener(MouseEvent.CLICK,clickBtnHandler);
				if(sp3.getChildByName("btn"+i))sp3.getChildByName("btn"+i).addEventListener(MouseEvent.CLICK,clickBtnHandler);
				if(sp4.getChildByName("btn"+i))sp4.getChildByName("btn"+i).addEventListener(MouseEvent.CLICK,clickBtnHandler);
				
				if(sp1.getChildByName("btn"+i) is TextField){
					sp1.getChildByName("btn"+i).visible=false;
					(sp1.getChildByName("btn"+i) as TextField).selectable=false;
//					this.toButtonMode( sp1.getChildByName("btn"+i));
				}
				if(sp2.getChildByName("btn"+i) is TextField){
					(sp2.getChildByName("btn"+i) as TextField).visible=false;
					(sp2.getChildByName("btn"+i) as TextField).selectable=false;
//					this.toButtonMode( sp2.getChildByName("btn"+i));
				}	
				if(sp3.getChildByName("btn"+i) is TextField){
					(sp3.getChildByName("btn"+i) as TextField).visible=false;
					(sp3.getChildByName("btn"+i) as TextField).selectable=false;
//						this.toButtonMode( sp3.getChildByName("btn"+i));
				}
			}
		}
		
		public function removeBtnsListener():void
		{
			var sp1:Sprite	=	this.getChildByName("content_xykd") as Sprite;
			var sp2:Sprite	=	this.getChildByName("content_qrdl") as Sprite;
			var sp3:Sprite	=	this.getChildByName("content_wscq") as Sprite;
			var sp4:Sprite	=	this.getChildByName("content_wljs") as Sprite;
			
			for(var i:uint=1; i<5; i++)
			{
				if(sp1.getChildByName("btn"+i))sp1.getChildByName("btn"+i).removeEventListener(MouseEvent.CLICK,clickBtnHandler);
				if(sp2.getChildByName("btn"+i))sp2.getChildByName("btn"+i).removeEventListener(MouseEvent.CLICK,clickBtnHandler);
				if(sp3.getChildByName("btn"+i))sp3.getChildByName("btn"+i).removeEventListener(MouseEvent.CLICK,clickBtnHandler);
				if(sp4.getChildByName("btn"+i))sp4.getChildByName("btn"+i).removeEventListener(MouseEvent.CLICK,clickBtnHandler);
			}
		}
		
		/**
		 *设置按扭 
		 * @param btnId
		 * @param className
		 * 
		 */
		public function setListBtn(btnId:String,className:String):void
		{
			var btnsp:Sprite	=	(this["listBtn"+btnId] as Sprite);
			btnsp.addChild( new ( getDefinitionByName(className) as Class))

			
			var lt:litiao = new litiao();
			lt.x	=	btnsp.x	+ 18.8;
			lt.y	=	btnsp.y	- 49.15;
			
			var lt2:litiao = new litiao();
			lt2.x	=	btnsp.x	+ 109.8;
			lt2.y	=	btnsp.y	- 49.15;
			
			this._list.addChild(btnsp);
			this._list.addChild(lt);
			this._list.addChild(lt2);
			
			this._list.setChildIndex( lt,0);
			this._list.setChildIndex( lt2,0);
			this._list.setChildIndex( btnsp,0);
		}
		
		/**
		 *清空list 
		 * 
		 */
		public function cleanList():void
		{
			setGlowFilter(1,true);
			while( this._list.numChildren)this._list.removeChildAt(0);
		}
		
		public function setContent(cc:String):void
		{
			this.getChildByName("content_xykd").visible=false;
			this.getChildByName("content_qrdl").visible=false;
			this.getChildByName("content_wscq").visible=false;
			this.getChildByName("content_xfsdl").visible=false;
			this.getChildByName("content_wljs").visible=false;
			this.getChildByName("content_yzwd").visible=false;
			this.getChildByName(cc).visible	=true;

			var string:String	=	getQualifiedClassName(this.getChildByName(cc));
			string=string.replace("::",".");//替换字符 :: 为 .
			string=string.substr(string.lastIndexOf(".")+1);//提取 . 后边的字符 就是类名

			switch(string){
				case "ContentDieScore":
					(this.getChildByName(cc) as ContentDieScore).init();
					(this.getChildByName(cc) as ContentDieScore).setCount( this.jsdiecount);
					(this.getChildByName(cc) as ContentDieScore).setItemsVisible( this.jsdieNoReward, this.jsdieYesReward);
					break;
				case "ContentXFSL":
					(this.getChildByName(cc) as ContentXFSL).init();
					(this.getChildByName(cc) as ContentXFSL).setCount( this.xfCount);
					break;
			}
		}
		
		public function setGlowFilter(num:uint,isnull:Boolean=false):void
		{
			var filter:GlowFilter=new GlowFilter(0x000000,0.8,20,20,2,BitmapFilterQuality.LOW);
			var filters:Array=[filter];
			
			listBtn1.filters	=	null;
			listBtn2.filters	=	null;
			listBtn3.filters	=	null;
			listBtn4.filters	=	null;
			
//			listBtn1.alpha	=	.5;
//			listBtn2.alpha	=	.5;
//			listBtn3.alpha	=	.5;
			if(!isnull)
			(this["listBtn"+num] as DisplayObject ).filters	=	filters; 
		}
		
		//************************************************  事件处理 *********************
		
		public function clickBtnHandler1(e:Event):void
		{
			this.nowSelectListObj	=	(movementAry[ (nowPage-1)*4] as Object);
			setContent( this.nowSelectListObj.contentClass);
			setGlowFilter(1);
		}
		
		public function clickBtnHandler2(e:Event):void
		{
			this.nowSelectListObj	=	(movementAry[ (nowPage-1)*4+1] as Object);
			setContent( this.nowSelectListObj.contentClass);
			setGlowFilter(2);
		}
		
		public function clickBtnHandler3(e:Event):void
		{
			this.nowSelectListObj	=	(movementAry[ (nowPage-1)*4+2] as Object);
			setContent( this.nowSelectListObj.contentClass);
			setGlowFilter(3);
		}
		
		public function clickBtnHandler4(e:Event):void
		{
			this.nowSelectListObj	=	(movementAry[ (nowPage-1)*4+3] as Object);
			setContent( this.nowSelectListObj.contentClass);
			setGlowFilter(4);
		}
		
		public function clickLeftBtnHandler(e:Event):void
		{
			nowPage	=	nowPage==1?1:nowPage-1;
			this.setMovement(this.movementAry);
		}
		
		public function clickRightBtnHandler(e:Event):void
		{
			nowPage	=	nowPage==maxPage?maxPage:nowPage+1;
			this.setMovement(this.movementAry);
		}
		
		public function clickCloseHandler(e:Event):void
		{
			this.dispatchEvent( new Event("close",true));
		}
		
		public function clickBtnHandler(e:Event):void
		{
			this.dispatchEvent( new MovementEvent("btnClick", this.nowSelectListObj, e.target.name, true));
		}
		
		public function toButtonMode(dis:DisplayObject):void
		{
						
//			dis.addEventListener(MouseEvent.ROLL_OVER, function(e:Event):void{
//				Mouse.cursor	=	MouseCursor.BUTTON;
//			});
//			dis.addEventListener(MouseEvent.ROLL_OUT, function(e:Event):void{
//				Mouse.cursor	=	MouseCursor.ARROW;
//			});
		}
		
	}
}

